package com.sojson.util.log;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;

import com.sojson.constant.ConstantByMap;
import com.sojson.constant.IpConfig;
import com.sojson.util.StringUtil;
import com.sojson.util.servlet.ServletUtil;
import com.sojson.util.token.TokenUtil;

/**
 * 日志工具类
 * 
 * @author liu
 * @date 2021-09-03
 */
public class LogUtil {

    /**
     * 将日志打印到控制台
     * 
     * @param log
     * @param request
     * @throws IOException 
     */
    public static void outConsole(Logger log) throws IOException {
        outConsole(log, ServletUtil.getHttpServletRequest());
    }

    /**
     * 将日志打印到控制台
     * 
     * @param log
     * @param request
     * @throws IOException 
     */
    public static void outConsole(Logger log, HttpServletRequest request) throws IOException {
        log.info("=======================================================");
        log.info("=======================================================");
        log.info("=======================================================");
        // 输出访问的项目
        log.info("访问项目: {}", IpConfig.url);
        // 输出访问的路径
        log.info("访问路径: {}", ServletUtil.getRequestUri(request));
        String errMapping = ServletUtil.getErrMapping(request);
        if (StringUtil.isNotBlankString(errMapping)) {
            log.info("访问路径: {}", errMapping);
        }
        // 输出来源IP
        log.info("来源IP : {}", ServletUtil.getTargetIp(request));
        // 输出用户
        if (TokenUtil.isLogin()) {
            log.info("用户ID : {}", TokenUtil.getUserId());
            log.info("用户角色: {}", TokenUtil.getRoleNames());
        } else {
            log.info("用户ID : 用户未登录");
        }
        // Token
        log.info("Token : {}", ServletUtil.getReqHeader(ConstantByMap.TOKEN_NAME));

        // 输入流的参数
        String paramBody = ServletUtil.getParamBody();
        log.info("=======");
        if (StringUtil.isNotBlankString(paramBody)) {
            log.info("body: {}", paramBody);
        } else {
            log.info("body: {}");
        }

        // 普通参数
        Map<String, String[]> parameterMap = ServletUtil.getParameterMap();
        log.info("=======");
        if (StringUtil.isNotBlankObject(parameterMap) && parameterMap.size() > 0) {
            Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
            for (Entry<String, String[]> entry : entrySet) {
                Object val = null;
                String[] value = entry.getValue();
                if (StringUtil.isNotBlankObject(value)) {
                    int valLen = value.length;
                    if (valLen == 0) {
                        val = "";
                    } else if (valLen == 1) {
                        val = value[0];
                    } else {
                        val = value;
                    }
                }
                log.info("{}: {}", entry.getKey(), val);
            }
        }
    }

}